package top.suven.base.http.admin.tools.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.suven.base.core.mybatis.PageData;
import top.suven.base.http.admin.frame.config.ResponseResult;
import top.suven.base.http.admin.sec.service.MenuService;
import top.suven.base.http.admin.tools.code.BuildCodeUtil;
import top.suven.base.http.admin.tools.service.BuildCodeService;
import top.suven.base.http.admin.tools.vo.request.BuildCodeRequestVo;
import top.suven.base.http.admin.tools.vo.request.BuildCodeViewRequestVo;
import top.suven.base.http.admin.tools.vo.response.BuildCodeResponseVo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Collections;
import java.util.List;

/**
 * @ClassName: 生成代码控制器
 * @Description:
 * @Author lixiangling
 * @Date 2018/7/10 11:57
 * @Copyright: (c) 2018 gc by https://www.gc.com.cn/
 * @Version : 1.0.0
 * --------------------------------------------------------
 * modifyer    modifyTime                 comment
 * 
 * --------------------------------------------------------
 */
@Controller
@RequestMapping("/buildCode")
public class BuildCodeController {
    private final static Logger logger = LoggerFactory.getLogger(BuildCodeController.class);
    @Autowired
    private BuildCodeService buildCodeService;
    @Autowired
    private MenuService menuService;
    @Value("${gc.developer.code.path:}")
    private String codePath;
    @Value("${gc.developer.sql.path:}")
    private String sqlPath;
    /**
     * @Title: 跳转登录界面主页
     * @Description: 
     * @param   
     * @return  
     * @throw 
     * @author lixiangling
     * @date 2018/7/10 12:00
     *  --------------------------------------------------------
     *  modifyer    modifyTime                 comment
     *  
     *  --------------------------------------------------------
     */
    @RequestMapping(value = "")
    public String index(HttpServletRequest request, HttpServletResponse response, Model model){
        return "tools/buildCode";
    }
    /**
     * @Title: 获取列表
     * @Description: 
     * @param   
     * @return  
     * @throw 
     * @author lixiangling
     * @date 2018/7/10 14:01
     *  --------------------------------------------------------
     *  modifyer    modifyTime                 comment
     *  
     *  --------------------------------------------------------
     */
    @ResponseBody
    @RequestMapping(value = "getTableView")
    public PageData<BuildCodeResponseVo> getTableView(BuildCodeRequestVo buildCodeRequestVo){
        List<BuildCodeResponseVo> buildCodeColumns = buildCodeService.getBuildCodeByTableName(buildCodeRequestVo.getTableName());
        if(buildCodeColumns == null || buildCodeColumns.isEmpty()){
            return new PageData<BuildCodeResponseVo>(Collections.EMPTY_LIST,0);
        }
        return new PageData(buildCodeColumns, buildCodeColumns.size());
    }

    @ResponseBody
    @RequestMapping(value = "saveBuildCode")
    public ResponseResult saveBuildCode(HttpServletRequest request,BuildCodeRequestVo buildCodeRequestVo)throws Exception{
        buildCodeRequestVo.setCodePath(codePath);
        buildCodeRequestVo.setSqlPath(sqlPath);
        int maxMenuCount =  menuService.getMenuCountByParentMenuCode(new StringBuffer(buildCodeRequestVo.getModule()).append(":").append(buildCodeRequestVo.getModule()).toString());
        buildCodeRequestVo.setMaxMenuOrder(maxMenuCount);
        BuildCodeUtil.createCode(buildCodeRequestVo);
        return ResponseResult.ok();
    }

    /**
     * @Title: 获取列表
     * @Description:
     * @param
     * @return
     * @throw
     * @author lixiangling
     * @date 2018/7/10 14:01
     *  --------------------------------------------------------
     *  modifyer    modifyTime                 comment
     *
     *  --------------------------------------------------------
     */
    @ResponseBody
    @RequestMapping(value = "getAAA")
    public PageData<BuildCodeResponseVo> getAAA(List<BuildCodeViewRequestVo> buildCodeViews){
        System.out.println(buildCodeViews.size());
        return new PageData<BuildCodeResponseVo>(Collections.EMPTY_LIST,0);
    }
}
